package com.appointment.controller;

import java.text.DateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.appointment.dao.ConfirmDao;
import com.appointment.entity.Confirm;
import com.appointment.entity.confirmPage;
import com.appointment.util.formifEmpty;
/**
 * 打印证明信息
 * @author WSD
 *
 */
@Controller
public class ConfirmController {
	
	@Resource
	ConfirmDao confirmdao;
	
	@RequestMapping("/confirm")
	public String confirm(HttpServletRequest request){
		UUID s = UUID.randomUUID();
		String s1 = s.toString();
		request.getSession().setAttribute("Session", s1);
		return "confirm";
	}
	
	
	
	@Transactional(rollbackFor={Exception.class})
	@RequestMapping("/addConfirms")
	public String addConfirms(Confirm confirm, String num, Model model, HttpServletRequest request){
		String num1 = (String) request.getSession().getAttribute("Session");
		confirm.setMonth("8");
		if(!formifEmpty.confirmifEmpty(confirm)){
			return "redirect:/error";
		}
		if(num==null){
			model.addAttribute("message", "请不要重复上传");
			return "right";
		}
		if(!num.equals(num1)){
			model.addAttribute("message", "请不要重复上传");
			return "right";
		}
		//判断姓名是否符合
//		if(!inputhanzi.inputhz(confirm.getUsername())){
//			model.addAttribute("message", "姓名输入不准确");
//			return "confirm";
//		}
		//判断是否同时操作
		Confirm confirm1 = confirmdao.findConfirmedByStuNumber(confirm.getStu_number());
		//不能操作两次
		if(confirm1!=null){
			model.addAttribute("message", "你已经上传");
			return "right";
		}
		//上传时间
		Date date = new Date();
		//String s = DateFormat.getDateInstance(DateFormat.DEFAULT).format(date);
		confirm.setTime(date);
		request.getSession().removeAttribute("Session");
		//添加信息
		confirmdao.addConfirm(confirm);
		model.addAttribute("message", "上传成功");
		return "right";
	}
	
	
}
